package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstants.ORDER_FAIL_GROUP,
        topic = MQConstants.SECKILL_ORDER,
        selectorExpression = MQConstants.ORDER_FAIL_TAG
)
@Component
public class OrderFailMQListener implements RocketMQListener<Map> {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Override
    public void onMessage(Map  map) {
        System.out.println("进入失败队列当中");
        Object obj =  map.get("seckillId");
        Long goodId=null;
        //根据不同的类型去处理
        if(obj instanceof  Long){
            goodId=(Long)obj;
        }else if(obj instanceof  Integer){
            goodId=((Integer)((Integer) obj)).longValue();
        }
        seckillGoodService.syncRedisCount(goodId);
        rocketMQTemplate.syncSend(MQConstants.ORDER_FAIL_NOTIFY_DEST,goodId);
    }
}